package test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by urfresh.mark on 2017/2/27.
 */
public class MongoSave {
    
    public MongoClient mongClient() throws Exception {
        MongoClient client = new MongoClient(
                Arrays.asList(
                        new ServerAddress("120.26.8.17", 27017)),
                Arrays.asList(
                        MongoCredential.createCredential("app_event_track", "app_event_track", "601ae08bc3ee0f2".toCharArray())
                ),
                MongoClientOptions.builder()
//                        .socketKeepAlive(true)
                        .connectionsPerHost(10)
                        .threadsAllowedToBlockForConnectionMultiplier(10)
                        .connectTimeout(500000)
                        .maxWaitTime(500000)
//                        .requiredReplicaSetName(mongoReplicaSet)
//                        .readPreference(ReadPreference.secondaryPreferred())
                        .socketTimeout(100000)
                        .build());
        return client;
    }
    
    public SimpleMongoDbFactory mongoFactory() throws Exception {
        return new SimpleMongoDbFactory(mongClient(), "app_event_track");
    }
    
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoFactory());
    }
    
    public static void main(String[] args) throws Exception {
        MongoSave mongoSave = new MongoSave();
        MongoTemplate mongoTemplate = mongoSave.mongoTemplate();
        AtomicInteger i = new AtomicInteger();

        FileInputStream inputStream = null;
        Scanner sc = null;
        List<JSONObject> list = new ArrayList<>();
        int start = 0;
        int count = 0;
//        File newFile1 = new File("C:\\Users\\urfresh.mark\\Desktop\\new_fileaf_1.txt");
        try {
            inputStream = new FileInputStream("H:\\0611.log");
            sc = new Scanner(inputStream, "UTF-8");
            AtomicInteger c = new AtomicInteger();
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                JSONObject obj = JSON.parseObject(line);
                obj.put("createTime", obj.getString("submitTime"));
                list.add(obj);
                if(list.size()>=50){
                    if(start>count) {
                        list.clear();
                        continue;
                    }
                    mongoTemplate.insert(list, "event_track");
                    System.out.println("已执行数量："+(c.addAndGet(list.size())));
                    list.clear();
                }
            }
            // note that Scanner suppresses exceptions
            if (sc.ioException() != null) {
                throw sc.ioException();
            }
        } finally {
            if(list!=null && list.size()>0)
                mongoTemplate.insert(list, "event_track");

            if (inputStream != null) {
                inputStream.close();
            }
            if (sc != null) {
                sc.close();
            }
        }

//        Files.readLines(new File("C:\\Users\\urfresh.mark\\Desktop\\10.log"), Charset.forName("utf-8"))
//                .stream()
//                .forEach(s -> {
//                    JSONObject obj = JSON.parseObject(s);
//                    obj.put("createTime", obj.getString("submitTime"));
//                    mongoTemplate.save(obj.toJSONString(), "event_track");
//                    System.out.println(i.addAndGet(1)+","+obj);
//                });
    }
    
}
